﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Threading;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography;
using System.Linq;

using VirtualShare.Common;

namespace VirtualShare.Client
{
    public partial class Main : Form
    {
        byte[] buffer = new byte[2];

        TcpClient mainServer;
        NetworkStream mainStream;
        BinaryReader  mainReader;
        BinaryWriter  mainWriter;

        TcpListener p2pServer;

        public static Client client;
        List<Client> clients = new List<Client>();

        public Main()
        {
            InitializeComponent();

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            LogIn(textBox1.Text, textBox2.Text);
        }

        void LogIn(string user, string pass)
        {
            mainServer = new TcpClient();
            mainServer.Connect(Config.host, 2424);

            mainStream = mainServer.GetStream();
            mainReader = new BinaryReader(mainStream);
            mainWriter = new BinaryWriter(mainStream);

            mainWriter.Write((short)PacketType.Login);
            mainWriter.Write(user);
            mainWriter.Write(pass);

            if ((PacketType)mainReader.ReadInt16() == PacketType.LoginFail)
                return;

            client = new Client(mainReader);

            mainStream.BeginRead(buffer, 0, 2, new AsyncCallback(OnReceiveData), null);

            panel1.Hide();
        }

        void OnReceiveData(IAsyncResult asyncResult)
        {
            if (!mainStream.DataAvailable)
                Environment.Exit(0);

            mainStream.EndRead(asyncResult);
            switch ((PacketType)BitConverter.ToInt16(buffer, 0))
            {
                case PacketType.BuddyConnection:
                    clients.Add(new Client(mainReader));
                    break;
            }

            mainStream.BeginRead(buffer, 0, 2, new AsyncCallback(OnReceiveData), null);
        }

        public void OnP2PConnection(IAsyncResult asyncResult)
        {
            Connection p2pConnection = new Connection(p2pServer.EndAcceptTcpClient(asyncResult));

            switch ((PacketType)p2pConnection.Reader.ReadInt16())
            {
                case PacketType.MessageConnection:
                    int id = p2pConnection.Reader.ReadInt32();
                    clients.First(c => c.id == id).msgConnection = p2pConnection;
                    break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }

        private void ClientForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
                Environment.Exit(0);
        }

        private void Main_Load(object sender, EventArgs e)
        {

        }
    }
}
